<!DOCTYPE html>
<html xmlns:v-bind="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="utf-8">
    <title></title>
    <link href="/css/bootstrap.min.css" rel="stylesheet">
    <link href="/css/font-awesome.css" rel="stylesheet">
    <link href="/css/datatables.min.css" rel="stylesheet">
    <link href="/css/animate.css" rel="stylesheet">
    <link href="/css/style.css" rel="stylesheet">
    <script src="/js/jquery-3.1.1.min.js"></script>
    <script src="/js/bootstrap.min.js"></script>
    <script src="/js/vue.js"></script>
</head>
<body style="background-color:whitesmoke">
<div id="admin">
    <div class="row wrapper border-bottom white-bg page-heading">
        <div class="col-lg-10">
            <h2>菜品管理</h2>
            <ol class="breadcrumb">
                <li>
                    <a href="index.html">菜品管理</a>
                </li>

            </ol>
        </div>
    </div>


    <div class="wrapper wrapper-content animated fadeInRight">
        <!-- 搜索功能  -->
        <div class="ibox-content m-b-sm border-bottom">
            <div class="row">
                <form role="form" class="form-horizontal">
                    <div class="form-group">

                        <label class=" col-sm-1 control-label">菜名</label>
                        <div class=" col-sm-2">
                            <input type="text" class="form-control" v-model="menu.menuName" placeholder="请输入菜名">
                        </div>

                        <label class=" col-sm-1 control-label">最低价</label>
                        <div class=" col-sm-2">
                            <input type="text" class="form-control" v-model="menu.minMoney" placeholder="请输入最低价">
                        </div>
                        <label class=" col-sm-1 control-label">最高价</label>
                        <div class=" col-sm-2">
                            <input type="text" class="form-control" v-model="menu.maxMoney" placeholder="请输入最高价">
                        </div>

                        <div>
                            <button class="btn btn-primary btn-sm" type="button"  @click="loadData()"><span class="glyphicon glyphicon-zoom-in" ></span> 查询</button>
                            <button class="btn btn-primary btn-sm" type="button" @click="add()"><span class="glyphicon glyphicon-plus"></span> 新增</button>
                        </div>
                    </div>
                </form>

            </div>

        </div>
        <div class="row">
            <div class="col-lg-12">

                <div class="ibox float-e-margins">

                    <div class="ibox-content">

                        <table class="footable table table-stripped toggle-arrow-tiny" data-page-size="15">
                            <thead>
                            <tr>

                                <th data-toggle="true">编号</th>
                                <th data-hide="phone">菜名</th>
                                <th data-hide="phone">价格</th>
                                <th data-hide="phone">部门</th>
                                <th data-hide="phone">详细信息</th>
                                <th  data-sort-ignore="true">操作</th>

                            </tr>
                            </thead>
                            <tbody>
                            <tr v-for="(x,index) in list">
                                <td>{{(curPage-1)*menu.row+index+1}}</td>
                                <td>{{x.menuName}}</td>
                                <td>{{x.menuMoney}}</td>
                                <td>{{x.category.categoryName}}</td>
                                <td>
                                    <!-- Single button -->
                                    <div class="btn-group">
                                        <button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                                            详细信息 <span class="caret"></span>
                                        </button>
                                        <ul class="dropdown-menu">
                                            <li><a href="#">{{x.batching.batchingOne}}:{{x.batching.batchingOneNum}}</a></li>
                                            <li><a href="#">{{x.batching.batchingTwo}}:{{x.batching.batchingTwoNum}}</a></li>
                                            <li><a href="#">{{x.batching.batchingThree}}:{{x.batching.batchingThreeNum}}</a></li>

                                        </ul>
                                    </div>
                                </td>
                                <td>
                                <button class="btn-primary btn btn-sm" @click="modifys(x)" ><span class="glyphicon glyphicon-tag"></span> 修改</button>
                                <button class="btn-primary btn btn-danger btn-sm"  @click="deleteData(x)" ><span class="glyphicon glyphicon-trash"></span> 删除</button>
                                </td>
                            </tr>
                            </tbody>
                        </table>

                        <div>
                            一共{{total}}条数据,一共{{totalPage}}页,当前第{{curPage}}页
                            <button class="btn btn-success btn-sm" type="button" @click="prePages()">上一页</button>
                            <button class="btn btn-success btn-sm" type="button" @click="nextPages()">下一页</button>
                            <button class="btn btn-success btn-sm" type="button" @click="indexPage()">首页</button>
                            <button class="btn btn-success btn-sm" type="button" @click="endPage()">尾页</button>
                            跳转到<input type="number" id="num" style="width: 40px" min="1" max="10" v-model="selectPage" @keyup="caiNum">页
                            <button class="btn btn-success btn-sm" type="button" @click="changePage()">跳转</button>
                            <select v-model="menu.row" @change="selectRow" >
                                <option value="3">每页3条</option>
                                <option value="5">每页5条</option>
                                <option value="10">每页10条</option>
                            </select>
                        </div>

                    </div>
                </div>
            </div>
        </div>
    </div>

<!--  修改模态框 -->
    <div id="modifys" class="modal fade" tabindex="-1" role="dialog">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title">{{title}}</h4>
                </div>
                <div class="modal-body">
                    <form role="form" class="form-horizontal">
                        <div class="form-group">
                            <label class=" col-sm-2 control-label">菜名</label>
                            <div class=" col-sm-10">
                                <input type="text" class="form-control" v-model="menu.menuName"  placeholder="请输入菜名">
                            </div>
                        </div>
                        <div class="form-group">
                            <label class=" col-sm-2 control-label">价格</label>
                            <div class=" col-sm-10">
                                <input type="text" class="form-control" v-model="menu.menuMoney"  placeholder="请输入价格">
                            </div>
                        </div>
                        <div class="form-group">
                            <label class=" col-sm-2 control-label">部门</label>
                            <div class=" col-sm-10">
                                <select v-model="menu.category.categoryId">
                                    <option  v-for="x in categoryList"  :value="x.categoryId" >{{x.categoryName}}</option>

                                </select>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class=" col-sm-2 control-label">原料1</label>
                            <div class=" col-sm-5">
                                <input type="text" class="form-control" v-model="menu.batching.batchingOne"  placeholder="请输入原料">
                            </div>
                            <div class=" col-sm-5">
                                <input type="text" class="form-control" v-bind:id="'batching'+menu.batching.batchingOneNum"  v-model="menu.batching.batchingOneNum" @keyup="caiNum(menu.batching.batchingOneNum)"   placeholder="请输入数量">
                            </div>
                        </div>
                        <div class="form-group">
                            <label class=" col-sm-2 control-label">原料2</label>
                            <div class=" col-sm-5">
                                <input type="text" class="form-control" v-model="menu.batching.batchingTwo"  placeholder="请输入原料">
                            </div>
                            <div class=" col-sm-5">
                                <input type="text" class="form-control" v-bind:id="'batching'+menu.batching.batchingTwoNum" v-model="menu.batching.batchingTwoNum" @keyup="caiNum(menu.batching.batchingTwoNum)"  placeholder="请输入数量">
                            </div>
                        </div>
                        <div class="form-group">
                            <label class=" col-sm-2 control-label">原料3</label>
                            <div class=" col-sm-5">
                                <input type="text" class="form-control" v-model="menu.batching.batchingThree"  placeholder="请输入原料">
                            </div>
                            <div class=" col-sm-5">
                                <input type="text" class="form-control" v-bind:id="'batching'+menu.batching.batchingThreeNum" v-model="menu.batching.batchingThreeNum" @keyup="caiNum(menu.batching.batchingThreeNum)"   placeholder="请输入数量">
                            </div>
                        </div>

                    </form>
                </div>
                <div class="modal-footer">
                    <button class="btn btn-primary btn-sm" type="button" @click="closeWin()"><span class="glyphicon glyphicon-remove"></span>关闭</button>
                    <button class="btn btn-primary btn-sm"  @click="saveAdmin"><span class="glyphicon glyphicon-save"   ></span>保存</button>
                </div>
            </div><!-- /.modal-content -->
        </div><!-- /.modal-dialog -->
    </div><!-- /.modal -->


</div>
</body>

<script>

    new Vue({
        el:"#admin",
        data:{
            list:[],
            total:0,//总条数
            totalPage:0,//总页数
            selectPage:1,//选择页数
            minMoney:0,  //最低价
            maxMoney:0,     //最高价
            menu:{ //查询对象
                page:1,
                row:3,
                menuId:"",  //菜名ID
                minMoney:"",//最低价
                maxMoney:"",//最高价
                menuName:"",  //菜名
                menuMoney:0,      //价格
                category:{
                    categoryName:"",    //部门
                    categoryId:"",  //种类编号
                },
                batching:{
                    //原料
                    batchingOne:"",
                    batchingOneNum:1,
                    batchingTwo:"",
                    batchingTwoNum:1,
                    batchingThree:"",
                    batchingThreeNum:1
                }
            },
            newcategory_name:"",  //新的种类
            categoryList:[],   //种类列表
            prePage:0,//上一页
            nextPage:0,//下一页.
            curPage:0,//当前页
            title:"用户修改",
            dd:true

        },
        methods:{
            loadData:function () {
                var self = this;
                //判断最低价和最高价是否成立
                if(self.menu.minMoney > self.menu.maxMoney){
                    self.menu.maxMoney = "";
                }
                $.ajax({
                    url:"http://192.168.7.36:2021/login-caipinguanli/menu/queryAll",//请求地址
                    type:"post",
                    // data:self.menu,
                    data:JSON.stringify(self.menu),
                    contentType:"application/json",
                    dataType:"json",//返回的数据类型
                    success:function (data) {
                        console.log(data);
                        self.list = data.list;
                        self.total = data.total;
                        self.totalPage = data.totalPage;
                        self.prePage = data.prePage;
                        self.nextPage = data.nextPage;
                        self.curPage = data.curPage;
                    }
                })
            },
            batchingData:function(){
                var self = this;
                $.ajax({
                    url:"http://192.168.7.36:2021/login-caipinguanli/category/categorylist",
                    type:"get",
                    dataType:"json",
                        success:function (data) {
                            self.categoryList = data.list;
                        }
                })
            },
            prePages:function () {//上一页
                this.menu.page = this.prePage;
                //刷新
                this.loadData();

            },
            nextPages:function () {
                this.menu.page = this.nextPage;
                //刷新
                this.loadData();

            },
            indexPage:function () {//首页
                this.menu.page = 1
                //刷新
                this.loadData();

            },
            endPage:function () {//尾页
                this.menu.page = this.totalPage;
                //刷新
                this.loadData();

            },
            changePage:function () {//跳转
                if(this.selectPage>this.totalPage){
                    this.menu.page = this.totalPage;
                }else{
                    this.menu.page = this.selectPage;
                }
                //刷新
                this.loadData();

            },
            selectRow:function () {//选择条数
                this.menu.page=1
                //刷新
                this.loadData();

            },
            valNum:function () {
                //定义正则表达式对象
                var dd =/^[\u4e00-\u9fa5]{0,}$/
                //如果输入的汉字，就显示1
                if(dd.test(this.selectPage)) {
                    this.selectPage=1
                }else{
                    console.log("=="+this.selectPage)
                    //如果输入的值大于总页数，就回到最大值
                    if(this.selectPage>this.totalPage){
                        this.selectPage = this.totalPage;
                    }
                }
            },
            //修改菜品
            modifys:function(x){
                var self = this;
                self.modifysHX(x);
                console.log(self.menu.category.categoryId)
                self.title = "菜品修改";
                //打开模态框
                $("#modifys").modal("show");
            },
            modifysHX:function(x){
                var self =  this;
                //数据回显
                // for(var one of self.list){
                //     if(one.menuId == x.menuId){
                        self.menu.menuId = x.menuId;  //菜名ID
                        self.menu.menuName = x.menuName;  //菜名
                        self.menu.menuMoney = x.menuMoney;      //价格
                        self.menu.category.categoryName = x.category.categoryName;    //部门
                        self.menu.category.categoryId = x.category.categoryId; //种类编号

                        //原料
                        self.menu.batching.batchingOne = x.batching.batchingOne
                        self.menu.batching.batchingOneNum = x.batching.batchingOneNum;
                        self.menu.batching.batchingTwo = x.batching.batchingTwo;
                        self.menu.batching.batchingTwoNum = x.batching.batchingTwoNum;
                        self.menu.batching.batchingThree = x.batching.batchingThree;
                        self.menu.batching.batchingThreeNum = x.batching.batchingThreeNum;
                    // }
                // }
            },
            //菜品添加
            add:function(){
                var self = this;
                self.menu.menuId = "";  //菜名ID
                self.menu.menuName = "";  //菜名
                self.menu.menuMoney = "";      //价格
                self.menu.category.categoryName = "";    //部门
                self.menu.category.categoryId = ""; //种类编号

                //原料
                self.menu.batching.batchingOne = "";
                self.menu.batching.batchingOneNum = "";
                self.menu.batching.batchingTwo = "";
                self.menu.batching.batchingTwoNum = "";
                self.menu.batching.batchingThree = "";
                self.menu.batching.batchingThreeNum = "";
                self.title = "菜品添加";
                //打开模态框
                $("#modifys").modal("show");
            },
            caiNum:function(x){
                //定义正则表达式对象
                var dd =/\D/;   //非数字
                var self = this;
                var ids = x;
                if(dd.test(self.menu.batching.batchingOneNum)){
                    self.menu.batching.batchingOneNum = 1;
                }
                if(dd.test(self.menu.batching.batchingTwoNum)){
                    self.menu.batching.batchingTwoNum = 1;
                }
                if(dd.test(self.menu.batching.batchingThreeNum)){
                    self.menu.batching.batchingThreeNum = 1;
                }
            },

            saveAdmin:function () {//保存
                console.log(this.menu)
                var url="";
                if(this.title=="菜品修改"){
                    url="http://192.168.7.36:2021/login-caipinguanli/menu/update";
                }else{
                    url="http://192.168.7.36:2021/login-caipinguanli/menu/add";
                }
                var self = this;
                console.log(url);
                $.ajax({
                    url:url,
                    type:"post",
                    data:JSON.stringify(self.menu),
                    contentType:"application/json",
                    dataType:"json",
                    success:function (data) {
                        console.log(data);
                        alert(data.info);
                        if(data.info=="保存成功"){
                            self.menu.menuName = "";
                            //关闭模态框
                            self.closeWin();
                            //刷新
                            self.loadData();
                        }
                    }
                })

            },
            closeWin:function () {//关闭模态框
                $("#modifys").modal("hide");
                this.menu.menuName="";
            },
            //删除
            deleteData:function (data) {
                var self = this;
                var flag = confirm("确认要删除吗？");
                if(flag){
                    self.menu.menuId = data.menuId;
                    $.ajax({
                        url:"http://192.168.7.36:2021/login-caipinguanli/menu/deleteData",     //请求路径
                        type:"post",    //请求方式
                        data:JSON.stringify(self.menu),     //参数，转JSON的字符串
                        contentType:"application/json",     //参数类型
                        dataType:"json",       //返回值类型
                        success:function (data) {
                            alert(data.info);
                            //刷新
                            self.loadData();
                        }
                    })
                }

            }


        },
        mounted(){
            //运行loadData函数
            this.loadData();
            this.batchingData();
        }
    })

</script>
</html>
